package com.example.xuanke.Controller;

import com.example.xuanke.commton.BusinessException;
import com.example.xuanke.commton.WrapMapper;
import com.example.xuanke.commton.Wrapper;
import com.example.xuanke.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/student-courses")
public class StudentCourseController {

    @Autowired
    private StudentCourseService studentCourseService;

    /**
     * 学生选课
     */
    @PostMapping("/enroll")
    public Wrapper<Boolean> enrollCourse(
            @RequestParam Long studentId,
            @RequestParam Long courseId) {
        try {
            boolean result = studentCourseService.enrollCourse(studentId, courseId);
            return WrapMapper.ok(result);
        } catch (BusinessException e) {
            return WrapMapper.error(e.getMessage());
        } catch (Exception e) {
            return WrapMapper.error("选课失败：" + e.getMessage());
        }
    }

    /**
     * 学生退课
     */
    @DeleteMapping("/drop")
    public Wrapper<Boolean> dropCourse(
            @RequestParam Long studentId,
            @RequestParam Long courseId) {
        try {
            boolean result = studentCourseService.dropCourse(studentId, courseId);
            return WrapMapper.ok(result);
        } catch (BusinessException e) {
            return WrapMapper.error(e.getMessage());
        } catch (Exception e) {
            return WrapMapper.error("退课失败：" + e.getMessage());
        }
    }
} 